﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Comom.Project.ControllerCom;
using Dhtl.Service.IService;
using dai_hoc_tuong_lai.Areas.Admin.Models;
using DataBase.Daihoctuonglai;
using FarmeWork.Helper;

namespace dai_hoc_tuong_lai.Areas.Admin.Controllers
{
     [Authorize(Roles = "Admin")]
    public class HinhAnhController : UTController
    {
        //
        // GET: /Admin/DichVu/

        private readonly IAlbum _iDichvu;
        private readonly ICateAlbum _iCateDichvu;
        private readonly int  PageSize = 15;

        public HinhAnhController(IAlbum iDichvu, ICateAlbum iCateDichvu)
        {
            this._iCateDichvu = iCateDichvu;
            this._iDichvu = iDichvu;
        }

        #region Chuyên mục

        public ActionResult CateIndex()
        {
            return View();
        }

        public ActionResult CateList()
        {

            var listEntity = _iCateDichvu.List();
            List<CateAlbumModel> model = new List<CateAlbumModel>();
            try
            {
                foreach (var item in listEntity)
                {
                    CateAlbumModel ml = new CateAlbumModel();
                    CateAlbumModel.MapFrom(item, ref  ml);
                    model.Add(ml);
                }
            }
            catch { }

            return PartialView("_CateList", model);
        }

        public ActionResult AddCate()
        {
            CateAlbumModel model = new CateAlbumModel();
            model.Id = 0;
            model.DateCreate = DateTime.Now;
            return PartialView("_AddCate", model);
        }

        public ActionResult UpdateCate(int id)
        {
            CateAlbumModel model = new CateAlbumModel();
            var entity = _iCateDichvu.GetById(id);
            try
            {
                CateAlbumModel.MapFrom(entity, ref model);
            }
            catch { }

            return PartialView("_UpdateCate", model);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult AddOrUpdateCate(CateAlbumModel model)
        {

            try
            {
                if (string.IsNullOrEmpty(model.Name))
                {
                    return JsonError("Nhập tên chuyên mục Album vào");
                }

                var entity = new Cate_Album();
                CateAlbumModel.MapFrom(model, ref entity);
                _iCateDichvu.AddOrUpdate(entity);

                return JsonSuccess(Url.Action("CateIndex"), null);
            }
            catch (Exception ex)
            {
                return JsonError(ex.ToString());
            }
        }

        public ActionResult DeleteCate(int id)
        {
            try
            {
                _iCateDichvu.delete(id);
                return JsonSuccess(Url.Action("CateIndex"), null);
            }
            catch (Exception ex)
            {
                return JsonError(ex.ToString());
            }
        }

        #endregion

        #region chi tiết


        public ActionResult Index()
        {
            var c = new List<Cate_Album>();
            c.Add(new Cate_Album { Id = 0, Name = "[Tất cả]" });
            var gh = _iCateDichvu.List().ToList();

            if (c.Count() > 0)
            {
                foreach (var g in gh)
                {
                    c.Add(g);
                }
            }

            ViewData["ListCatethongtin"] = new SelectList(c, "Id", "Name");

            return View();
        }

        public ActionResult List(int cateID, int page)
        {

            var listEntity = _iDichvu.ListByCatetId(cateID);
            List<AlbumModel> model = new List<AlbumModel>();
            try
            {
                foreach (var item in listEntity)
                {
                    AlbumModel ml = new AlbumModel();
                    AlbumModel.MapFrom(item, ref  ml);
                    model.Add(ml);
                }
            }
            catch { }

            var ListPageSize = new PagedData<AlbumModel>();

            if (model.Count() > 0)
            {
                ListPageSize.Data = model.Skip(PageSize * (page - 1)).Take(PageSize).ToList();
                ListPageSize.NumberOfPages = Convert.ToInt32(Math.Ceiling((double)model.Count() / PageSize));
                ListPageSize.CurrentPage = page;
            }
            else
            {
                ListPageSize.Data = null;
                ListPageSize.NumberOfPages = 0;
                ListPageSize.CurrentPage = 0;
            }

            return PartialView("_List", ListPageSize);
        }

        public ActionResult Add()
        {
            var c = new List<Cate_Album>();
            c.Add(new Cate_Album { Id = 0, Name = "[Tất cả]" });
            var gh = _iCateDichvu.List().ToList();

            if (c.Count() > 0)
            {
                foreach (var g in gh)
                {
                    c.Add(g);
                }
            }

            ViewData["ListCatethongtinAdd"] = new SelectList(c, "Id", "Name");

            AlbumModel model = new AlbumModel();
            model.Id = 0;
            model.DateCreate = DateTime.Now;
           
            return View("_Add", model);
        }

        public ActionResult Update(int id)
        {
            AlbumModel model = new AlbumModel();
            var entity = _iDichvu.GetById(id);
            try
            {
                AlbumModel.MapFrom(entity, ref model);
            }
            catch { }

            var c = new List<Cate_Album>();
            c.Add(new Cate_Album { Id = 0, Name = "[Tất cả]" });
            var gh = _iCateDichvu.List().ToList();

            if (c.Count() > 0)
            {
                foreach (var g in gh)
                {
                    c.Add(g);
                }
            }

            ViewData["ListCatethongtinAdd"] = new SelectList(c, "Id", "Name", entity.CateId);

            return PartialView("_Update", model);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult AddOrUpdate(AlbumModel model)
        {

            try
            {
                if (model.CateId == 0)
                {
                    return JsonError("Chọn chuyên mục cho Album");
                }

                if (string.IsNullOrEmpty(model.Name))
                {
                    return JsonError("Nhập tên cho hình ảnh");
                }

                if (string.IsNullOrEmpty(model.Mota))
                {
                    return JsonError("Nhập mô tả cho ảnh vào vào");
                }

                if (string.IsNullOrEmpty(model.Anh))
                {
                    return JsonError("Vui lòng upload ảnh");
                }


                var entity = new Album();
                AlbumModel.MapFrom(model, ref entity);
                _iDichvu.AddOrUpdate(entity);

                return JsonSuccess(Url.Action("Index"), null);
            }
            catch (Exception ex)
            {
                return JsonError(ex.ToString());
            }
        }

        public ActionResult Delete(int id)
        {
            try
            {
                _iDichvu.delete(id);
                return JsonSuccess(Url.Action("Index"), null);
            }
            catch (Exception ex)
            {
                return JsonError(ex.ToString());
            }
        }

        #endregion



    }
}
